<?php
class ControlLogin extends Controller {
	var $data;

	function ControlLogin()
	{
		parent::Controller();
		$this->load->model('Login');
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		
		$this->load->database();	
                session_start();
		
	}
	
	function index()
	{
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('umum/login/log');
		$this->load->view('layout/footer');
	}
	
	function prosesLogin() {
		$this->data['title']='Home';
		$username = $this->input->post("username");
		$password = $this->input->post("password");
		$ax = md5($password);
		//insert log
		if(getenv("HTTP_CLIENT_IP")) { 
			$ipad = getenv("HTTP_CLIENT_IP"); 
		} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
			$ipad = getenv("HTTP_X_FORWARDED_FOR"); 
		} else { 
			$ipad = getenv("REMOTE_ADDR"); 
		}
		$ipad = substr($ipad, 0,14);
		$translog_id = $this->Login->show_id_trans_log()->row();
		$this->xd = $translog_id->id + 1;
		$this->tgl = date('Y-m-d H:i:s');
		
		$id = $this->Login->check_user($username,$password)->result();
		foreach($id as $xp) {
		if ($xp->user_right ==1) {
		  $_SESSION["sys_admin"] = 1;
		  $_SESSION["name"] = $xp->user_id;
		  $insert_log = array('id' => $this->xd,
							'user_id' =>$xp->user_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('login'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
			//insert ke table trans log
			$this->Login->insert_log($insert_log);
			
			//set template
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dashboard/dashBoard');
			$this->load->view('layout/slice_bawah');
		  
		}
		else if($xp->user_right ==2) {
			$_SESSION["admin"] = 2;
			$_SESSION["name"] = $xp->user_id;
			$insert_log = array('id' => $this->xd,
							'user_id' =>$xp->user_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('login'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
			//insert ke table trans log
			$this->Login->insert_log($insert_log);
			
			//set template
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dashboard/dashBoard');
			$this->load->view('layout/slice_bawah');

		}
		else if($xp->user_right ==3) {
			$_SESSION["intern_audit"] = 3;
			$_SESSION["name"] = $xp->user_id;
			$insert_log = array('id' => $this->xd,
							'user_id' =>$xp->user_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('login'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
			//insert ke table trans log
			$this->Login->insert_log($insert_log);
			
			//set template
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dashboard/dashBoard');
			$this->load->view('layout/slice_bawah');

		}
		else if($xp->user_right ==4) {
			$_SESSION["second_consult"] = 4;
			$_SESSION["name"] = $xp->user_id;
			$insert_log = array('id' => $this->xd,
							'user_id' =>$xp->user_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('login'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
			//insert ke table trans log
			$this->Login->insert_log($insert_log);
			
			//set template
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dashboard/dashBoard');
			$this->load->view('layout/slice_bawah');

		}
		else if($xp->user_right ==5) {
			$_SESSION["dept_pic"] = 5;
			$_SESSION["name"] = $xp->user_id;
			$insert_log = array('id' => $this->xd,
							'user_id' =>$xp->user_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('login'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
			//insert ke table trans log
			$this->Login->insert_log($insert_log);
			
			//set template
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('dashboard/dashBoard');
			$this->load->view('layout/slice_bawah');

		}
		else {
			redirect('main');
		}
				
		} 
		
		$cek = $this->Login->check_user($username,$password)->row();
		if((isset($cek->name) == "") || (isset($cek->pass) == "")) {
				$this->data['link'] = array('link_back' =>anchor('main','kembali',array('class' => 'back')));
			
			$this->load->vars($this->data);
	
			$this->load->view('layout/head', $this->data);
			$this->load->view('layout/body', $this->data);
			$this->load->view('main', $this->data);
			$this->load->view('layout/footer', $this->data);	
			}
		
		
		if(($username and $password) == "") {
		
		$this->data['link'] = array('link_back' =>anchor('main','kembali',array('class' => 'back')));
		
		
		}
		
  }
}
?>
